package com.hkfrich.common.utils;

/**
 * @ClassName ResultUtil
 * @Desscription
 * @Author _HeiShuai
 * @Date 2022/8/17 15:59
 * @Version 1.0
 **/



import com.alibaba.fastjson.JSON;
import com.hkfrich.common.enums.CodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultUtils{

    private int code;

    private String msg;

    private Object data;


    public static void renderFailJson(HttpServletResponse response,String msg) throws IOException {
        String s = JSON.toJSONString(resultData(CodeEnum.TIMEOUT.statusCode(),msg,null));
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type","application/json");
        response.getOutputStream().write(s.getBytes());
    }

    public static ResultUtils success(String msg) {
        return resultData(CodeEnum.SUCCESS.statusCode(), msg, null);
    }

    public static ResultUtils success(Object data, String msg) {
        return resultData(CodeEnum.SUCCESS.statusCode(), msg, data);
    }

    public static ResultUtils fail(String msg) {
        return resultData(CodeEnum.ERROR.statusCode(), msg, null);
    }

    public static ResultUtils fail(Object data, String msg) {
        return resultData(CodeEnum.ERROR.statusCode(), msg, data);
    }

    private static ResultUtils resultData(int code, String msg, Object data) {
        ResultUtils resultData = new ResultUtils();
        resultData.setCode(code);
        resultData.setMsg(msg);
        resultData.setData(data);
        return resultData;
    }

}


